//
//  Controller05.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/1.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

class Controller05: UIViewController,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    let datas = Interest.createInterests();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI();
    }

    func configUI() {
        do{  //bg View
            let imageView = UIImageView(frame: UIScreen.main.bounds);
            view.addSubview(imageView);
            imageView.image = UIImage(named: "blue");

            let brurEffect = UIBlurEffect(style: .light);
            let visualView = UIVisualEffectView(effect: brurEffect);
            visualView.frame = CGRect(x: 0, y: 0, width: SCREEN_W, height:  SCREEN_H);
            view.addSubview(visualView);
        }
        
        do{
            let layout = UICollectionViewFlowLayout();
            layout.scrollDirection = .horizontal;
            layout.itemSize = CGSize(width: SCREEN_W - 80, height: SCREEN_H - 160);
            layout.minimumLineSpacing = 0;
            layout.sectionInset = UIEdgeInsetsMake(0, 40, 0, 0)
            let collectView = UICollectionView(frame:CGRect(x: 0, y: 100, width: SCREEN_W, height: SCREEN_H - 200) , collectionViewLayout: layout);
            collectView.delegate = self;
            collectView.dataSource = self;
            view.addSubview(collectView);
            collectView.backgroundColor = .clear;
            collectView.register(UINib(nibName: "CarouselCell", bundle: nil), forCellWithReuseIdentifier: "aaa");
        }
        
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    //MARK: - delegate & dataSource
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "aaa", for: indexPath) as! CarouselCell;
        cell.obj = datas[indexPath.row];
        return cell;
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return datas.count;
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if !decelerate {
            var p = scrollView.contentOffset;
            p.x = scrollToTarget(sv: scrollView , offset: p);
            scrollView.contentOffset = p;
        }
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        var p = targetContentOffset.pointee;
        p.x = scrollToTarget(sv: scrollView ,offset: p);
        targetContentOffset.pointee = p;
    }
    
    
    //不具备通用性。
    func scrollToTarget(sv : UIScrollView, offset:CGPoint) -> CGFloat{
        let oneWidth : CGFloat = SCREEN_W - 80;
        let n = floor((offset.x  + oneWidth * 0.5) / oneWidth);
        return oneWidth * n  ;
    }
    
}
